<?php

namespace App\Models\Entity\Asset\Vlive;

use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * Class Vchan
 * @package App\Models\Entity\Asset\Vlive
 * @version December 11, 2017, 10:26 am CST
 *
 * @property \Illuminate\Database\Eloquent\Collection Vcposter
 * @property \Illuminate\Database\Eloquent\Collection Vcstream
 * @property \Illuminate\Database\Eloquent\Collection Vepg
 * @property \Illuminate\Database\Eloquent\Collection vchanSps
 * @property \Illuminate\Database\Eloquent\Collection vchanVccats
 * @property \Illuminate\Database\Eloquent\Collection vchanVcareacats
 * @property \Illuminate\Database\Eloquent\Collection vchanVlangs
 * @property string sp_id
 * @property string vchan_no
 * @property string name
 * @property string title
 * @property string subtitle
 * @property string spell
 * @property string short_desc
 * @property string long_desc
 * @property string vccat_id
 * @property string vcareacat_id
 * @property string vlang_id
 * @property string vlogo
 * @property string Vcposter
 * @property string Vepg
 * @property string Vcstream
 * @property timestamp created_at
 * @property timestamp updated_at
 * @property timestamp expiring_at
 * @property timestamp online_at
 * @property timestamp offline_at
 * @property timestamp published_at
 */
class Vchan extends Model
{
    use SoftDeletes;


    public $table = 'vchans';
    

    protected $dates = ['deleted_at'];


    public $fillable = [
        // 'sp_id',
        'vchan_no',
        'name',
        'title',
        'subtitle',
        // 'spell',
        'short_desc',
        'long_desc',
        'vccat_id',
        // 'vcareacat_id',
        'vlang_id',
        'vlogo',
        'Vcposter',
        'Vepg',
        'Vcstream',
        'listorder',
        'charge_type',
        'created_at',
        'updated_at',
        'expiring_at',
        'start_at',
        'online_at',
        'offline_at',
        'published_at'
    ];

    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        // 'sp_id' => 'string',
        'vchan_no' => 'string',
        'name' => 'string',
        'title' => 'string',
        'subtitle' => 'string',
        // 'spell' => 'string',
        'short_desc' => 'string',
        'long_desc' => 'string',
        'vccat_id' => 'string',
        // 'vcareacat_id' => 'string',
        'vlang_id' => 'string',
        'vlogo' => 'string',
        'charge_type' => 'integer',
        'Vcposter' => 'string',
        'Vepg' => 'string',
        'listorder' => 'integer',
        'Vcstream' => 'string'
    ];

    /**
     * Validation rules
     *
     * @var array
     */
    public static $rules = [
        'sp_id' => 'required',
        'vchan_no' => 'required',
        'name' => 'required',
        'title' => 'required'
    ];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     **/
    public function vcposters()
    {
        return $this->belongsToMany(\App\Models\Entity\Asset\Vlive\Vcposter::class, 'vchan_vcposters', 'vchan_id', 'vcposter_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     **/
    public function vcstreams()
    {
        return $this->belongsToMany(\App\Models\Entity\Asset\Vlive\Vcstream::class, 'vchan_vcstreams', 'vchan_id', 'vcstream_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     **/
    public function vepgs()
    {
        return $this->belongsToMany(\App\Models\Entity\Asset\Vlive\Vepg::class, 'vchan_vepgs', 'vchan_id', 'vepg_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     **/
    public function sps()
    {
        return $this->belongsToMany(\App\Models\Common\EntityData\Channel\Sp::class, 'vchan_sps', 'vchan_id', 'sp_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     **/
    public function vccat()
    {
        return $this->hasOne(\App\Models\Common\CatData\Video\Vccat::class, 'id', 'vccat_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     **/
    public function vcareacats()
    {
        return $this->belongsToMany(\App\Models\Common\CatData\Video\Vcareacat::class, 'vchan_vcareacats', 'vchan_id', 'vcareacat_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     **/
    public function vlangs()
    {
        return $this->belongsToMany(\App\Models\Common\Enumdata\Enumop\Video\Vlang::class, 'vchan_vlangs', 'vchan_id', 'vlang_id');
    }

    /**
     * 分类
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     **/
    public function vccattags()
    {
        return $this->belongsToMany(\App\Models\Common\CatData\Video\Vccattag::class, 'vchan_vccattags', 'vchan_id', 'vccattag_id');
    }

    /**
    * 过滤上下线数据
    **/
    public function scopeOnlineStatus($query, $status = 1)
    {
        return $query->where('online_status', $status);
    }

    /**
    * 过滤上线发布数据
    **/
    public function scopeOnlinePublished($query, $status = 1)
    {
        return $query->where(['online_status' => 1, 'publish_status' => $status]);
    }

    /**
    * 过滤下线发布数据
    **/
    public function scopeOfflinePublished($query, $status = 1)
    {
        return $query->where(['online_status' => 0, 'publish_down_status' => $status]);
    }

    /**
     * 获得此媒资的所有标签。
     */
    public function tags()
    {
        return $this->morphToMany('\App\Models\Entity\Tag\tag', 'tag_content');
    }
}
